/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.common.controller;

import com.koron.common.bean.KpiStatisticsBean;
import com.koron.common.domain.DataBean;
import com.koron.common.domain.KpiStatistics;
import com.koron.common.service.KpiStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;

import java.util.List;

@Api(tags = "kpiStatistics", description = "指标统计接口")
@RestController
@RequestMapping("/kpiStatistics")
public class KpiStatisticsController {
	
	@Autowired
	private KpiStatisticsService service;
	
    @ApiOperation("查询指标月度数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kpiId", value = "指标id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "kpiName", value = "指标name", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "kpiCode", value = "指标code", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "dateType", value = "日期类型:1月，2季，3年", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "dateValue", value = "日期 月：2020-01，季2020-1/2/3/4,年2020", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "statisticalType", value = "统计类型：1员工，2部门，3公司，4片区，5集团", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "objectId", value = "统计对象id", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "objectName", value = "统计对象name", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", dataType = "Integer")
    })
    @RequestMapping(value = "/monthDataList.htm", method = {RequestMethod.GET})
    public MessageBean<?> monthDataList(String kpiId,String kpiName,String kpiCode,Integer dateType,String dateValue,Integer statisticalType,String objectId,String objectName,Integer page,Integer pageSize) {
        MessageBean<DataBean> msg = new MessageBean<>();
        Integer totalPage = null;
	    Integer totalNumber = null;
	    Integer start = null;
	    Integer end = null;
	    if (null!=page && pageSize!=null){
            if (page >0 && pageSize > 0){
            }else{
            	//第一页 10条数据
            	page =1;
            	pageSize=10;
            }
            start=(page<=1)?0:(page-1)*pageSize;
            end = pageSize;
        }
	    KpiStatisticsBean bean = new KpiStatisticsBean();
	    bean.setKpiId(kpiId);
	    bean.setKpiName(kpiName);
	    bean.setKpiCode(kpiCode);
	    bean.setDateType(dateType);
	    bean.setDateValue(dateValue);
	    bean.setStatisticalType(statisticalType);
	    bean.setObjectId(objectId);
	    bean.setObjectName(objectName);
	    bean.setStart(start);
	    bean.setEnd(end);
        List<KpiStatistics> list = service.query(bean);
        totalNumber = service.count(bean);
        if(start != null && end != null){
			totalPage = (totalNumber+pageSize-1)/pageSize;
		}else{ 
			totalPage = 1;
			page = totalPage;
			pageSize = totalNumber;
		}
        DataBean dataBean = new DataBean<>();
        dataBean.setList(list);
        dataBean.setTotalNumber(totalNumber);
        dataBean.setTotalPage(totalPage);
        msg.setCode(0);
        msg.setDescription("S");
        msg.setData(dataBean);
        return msg;
    }
}
